package com.stox.client.ui.javafx.watchlist.view;

import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;

import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.ui.javafx.watchlist.widget.AddWatchlistButton;
import com.stox.client.ui.javafx.watchlist.widget.DeleteWatchlistButton;
import com.stox.client.ui.javafx.watchlist.widget.EditWatchlistButton;
import com.stox.client.ui.javafx.watchlist.widget.WatchlistComboBox;
import com.stox.client.ui.javafx.watchlist.widget.WatchlistEntryTableView;
import com.stox.client.workbench.ui.javafx.stage.StoxStage;
import com.stox.client.workbench.ui.javafx.view.View;
import com.stox.shared.i18n.MessageSource;
import com.stox.shared.model.WatchlistEntry;

public class WatchlistView extends View {

	private final WatchlistEntryTableView watchlistEntryTableView;
	
	public WatchlistView() {
		super(MessageSource.get("Watchlists"));
		
		WatchlistComboBox watchlistComboBox = new WatchlistComboBox();
		UI.style(watchlistComboBox, CSS.undecorated);
		BorderPane borderPane = new BorderPane();
		borderPane.setTop(createWatchlistPane(watchlistComboBox));
		this.watchlistEntryTableView = createWatchlistEntryPane(watchlistComboBox); 
		borderPane.setCenter(watchlistEntryTableView);
		setCenter(borderPane);
		
		setSize();
		setLocation();
	}
	
	private Node createWatchlistPane(WatchlistComboBox watchlistComboBox){
		HBox watchlistPane = new HBox( 
				new AddWatchlistButton(watchlistComboBox),
				watchlistComboBox,
				new EditWatchlistButton(watchlistComboBox),
				new DeleteWatchlistButton(watchlistComboBox)); 
		watchlistPane.setMaxWidth(Double.MAX_VALUE);
		HBox.setHgrow(watchlistComboBox, Priority.ALWAYS);
		watchlistComboBox.setMaxWidth(Double.MAX_VALUE);
		UI.style(watchlistPane, CSS.watchlistPane);
		return watchlistPane;
	}
	
	private WatchlistEntryTableView createWatchlistEntryPane(WatchlistComboBox watchlistComboBox){
		return new WatchlistEntryTableView(watchlistComboBox);
	}

	private void setSize(){
		setPrefHeight(StoxStage.getInstance().getContentPane().getHeight()/2);
		setPrefWidth(StoxStage.getInstance().getContentPane().getWidth()*2/5);
	}
	
	private void setLocation(){
		Bounds bounds = StoxStage.getInstance().getContentPane().getLayoutBounds();
		setLayoutX(bounds.getMaxX() - getPrefWidth());
		setLayoutY(bounds.getMaxY() - getPrefHeight());
	}
	
	public void addWatchlistEntry(WatchlistEntry watchlistEntry){
		watchlistEntryTableView.addWatchlistEntry(watchlistEntry);
	}
	
	public WatchlistEntryTableView getWatchlistEntryTableView() {
		return watchlistEntryTableView;
	}
}
